
package kaizen.jdbc;

import java.io.PrintStream;

import java.sql.*;

import java.util.Vector;

// Referenced classes of package kaizen.jdbc:
//            dbRecord

public class dbCursor
{

  public dbCursor(String s, ResultSet resultset, 
                  ResultSetMetaData resultsetmetadata)
  {
    ls_name = "";
    lb_status = false;
    ls_name = s;
    results = resultset;
    resultsMD = resultsetmetadata;
    lb_status = false;
  }

  public void First()
  {
    if (!lb_status)
      readResults();
    if (li_rowsCount > 0)
      li_currentRow = 1;
  }

  public void Last()
  {
    if (!lb_status)
      readResults();
    if (li_rowsCount > 0)
      li_currentRow = li_rowsCount;
  }

  public void Next()
  {
    if (!lb_status)
      readResults();
    if (li_rowsCount > 0 && li_currentRow < li_rowsCount)
      li_currentRow++;
  }

  public void Previous()
  {
    if (!lb_status)
      readResults();
    if (li_rowsCount > 0 && li_currentRow > 1)
      li_currentRow--;
  }

  public Vector getAllRecords()
  {
    if (!lb_status)
      readResults();
    return vRecord;
  }

  public String[] getAllRows()
  {
    if (!lb_status)
      readResults();
    return lsa_rows;
  }

  public String getName()
  {
    return ls_name;
  }

  public dbRecord getRecord()
  {
    dbRecord dbrecord = null;
    if (!lb_status)
      readResults();
    if (li_currentRow > 0)
      dbrecord = (dbRecord)vRecord.elementAt(li_currentRow - 1);
    return dbrecord;
  }

  public ResultSet getResults()
  {
    return results;
  }

  public ResultSetMetaData getResultsMD()
  {
    return resultsMD;
  }

  public String getRow()
  {
    String s = null;
    if (!lb_status)
      readResults();
    if (li_currentRow > 0)
      s = lsa_rows[li_currentRow - 1];
    return s;
  }

  private void readResults()
  {
    li_rowsCount = 0;
    vRecord = new Vector();
    try
    {
      int i = resultsMD.getColumnCount();
      try
      {
        while (results.next())
        {
          dbRecord dbrecord = new dbRecord(i);
          for (int k = 1; k <= i; k++)
          {
            String dataType = resultsMD.getColumnTypeName(k);
            if (dataType == null)
              dataType = "";
            String txt_result = "";
            txt_result = results.getString(k);
            dbrecord.setField(k, txt_result);
          }

          vRecord.addElement(dbrecord);
          li_rowsCount++;
        }
        lsa_rows = new String[li_rowsCount];
        for (int j = 0; j < li_rowsCount; j++)
          lsa_rows[j] = ((dbRecord)vRecord.elementAt(j)).getRow();

        lb_status = true;
        return;
      } catch (SQLException sqlexception1)
      {
        System.out.println("Error    -8: " + sqlexception1);
      }
      return;
    } catch (SQLException sqlexception)
    {
      System.out.println("Error    -9: " + sqlexception);
    }
  }

  String ls_name;
  int li_currentRow;
  int li_rowsCount;
  boolean lb_status;
  ResultSet results;
  ResultSetMetaData resultsMD;
  Vector vRecord;
  String lsa_rows[];
}
